package cn.yunyichina.provider.hquery.service.impl;

import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.hquery.thread.ExamineDetailRunnable;
import cn.yunyichina.provider.hquery.thread.ExamineListRunnable;
import cn.yunyichina.provider.hquery.vo.ExaminationVo;
import cn.yunyichina.provider.hquery.vo.ExamineListVoResp;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineDetailVo;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineDetailVoResponse;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineListVo;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineListVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2016/12/12 0012.
 * 检验检查功能
 */
@Service("checkbodyServiceImpl")
public class CheckbodyServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(CheckbodyServiceImpl.class);

    //编写Comparator,根据reportTime对ExamineListVoResponse进行排序
    private final Comparator<ExamineListVoResponse> COMPARATOR = new Comparator<ExamineListVoResponse>() {
       public int compare(ExamineListVoResponse o1, ExamineListVoResponse o2) {
           return o1.compareTo(o2);//运用User类的compareTo方法比较两个对象       
       }
    };
    
    /**
             检查结果列表查询接口
     */
    public ResponseObject listCheckbody(String data,String sessionId,String appId) {
    	
        logger.info("检查结果列表 listCheckbody data: "+data);
        ResponseObject response = new ResponseObject();
        ExaminationVo requestVo = JsonUtils.parseObject(data, ExaminationVo.class);
        
        if (StringUtils.isBlank(requestVo.getHospitalCode())
                ||StringUtils.isBlank(requestVo.getBeginDate())
                ||StringUtils.isBlank(requestVo.getEndDate())){
            response.setResultCodeAndMessage("18002","hospitalCode,beginDate,endDate不能为空");
            return response;
        }
        
        //结果集 检查列表信息
        List<ExamineListVoResponse> examineListVoResponseList = null;
        
        //检查列表查询
        try {
            ExamineListVo examineListVo = new ExamineListVo();
            examineListVo.setHospitalCode(requestVo.getHospitalCode());
            examineListVo.setBranchCode(requestVo.getBranchCode());
            examineListVo.setPatCardType(requestVo.getPatCardType());
            examineListVo.setPatCardNo(requestVo.getPatCardNo());
            examineListVo.setBeginDate(requestVo.getBeginDate());
            examineListVo.setEndDate(requestVo.getEndDate());
            examineListVo.setPatId(requestVo.getPatId());
            examineListVo.setAdmissionNo(requestVo.getAdmissionNo());
            logger.info("调用调度中心开始");
            ResCommon<List<ExamineListVoResponse>> examineList  = HisifaceService.getHisifaceClient().getExamineList(examineListVo);
            logger.info("调用调度中心结束");
            if (!"0".equals(examineList.getResultCode())){
                logger.info("调用调度中心产生异常"+ examineList.getResultMessage());
                response.setResultCodeAndMessage("18998", examineList.getResultMessage());
                return response;
            }
            logger.info("调用调度中心返回 "+JsonUtils.toJsonString(examineList));
            
            examineListVoResponseList = examineList.getResult();
            
        } catch (Exception ex) {
            response.setResultCodeAndMessage("18999","系统异常");
            return  response;
        }
        
        List<ExamineListVoResponse> emptyReportimeList = new ArrayList<ExamineListVoResponse>();
        List<ExamineListVoResponse> notEmptyReportimeList = new ArrayList<ExamineListVoResponse>();

        for(ExamineListVoResponse u : examineListVoResponseList){
            if("".equals(u.getReportTime()) || u.getReportTime() == null){
                emptyReportimeList.add(u);
            } else {
                notEmptyReportimeList.add(u);
            }
        }
        //用我们写好的Comparator对notEmptyReportimeList进行reprotTime日期升序排序
        Collections.sort(notEmptyReportimeList, COMPARATOR);
        examineListVoResponseList.clear();
        examineListVoResponseList.addAll(notEmptyReportimeList);
        examineListVoResponseList.addAll(emptyReportimeList);
        
        List<ExamineListVoResp> examineListVoRespList = new ArrayList<ExamineListVoResp>();
        if(CollectionUtils.isNotEmpty(examineListVoResponseList)){
            
        	ExamineListVoResp examineListVoResp = null;
            for(ExamineListVoResponse examineListVoResponse:examineListVoResponseList){
            	examineListVoResp = new ExamineListVoResp();
            	examineListVoResp.setCheckId(examineListVoResponse.getCheckId());
            	examineListVoResp.setCheckName(examineListVoResponse.getCheckName());
            	examineListVoResp.setCheckType(examineListVoResponse.getCheckType());
            	examineListVoResp.setCheckTypeName(examineListVoResponse.getCheckTypeName());
            	examineListVoResp.setOrderDoctorCode(examineListVoResponse.getOrderDoctorCode());
            	examineListVoResp.setOrderDoctorName(examineListVoResponse.getOrderDoctor());
            	examineListVoResp.setOrderDeptName(examineListVoResponse.getOrderDept());
            	examineListVoResp.setDeptName(examineListVoResponse.getDeptName());
            	examineListVoResp.setDoctorName(examineListVoResponse.getDoctorName());
            	examineListVoResp.setCheckTime(examineListVoResponse.getCheckTime());
            	examineListVoResp.setReportTime(examineListVoResponse.getReportTime());
            	examineListVoResp.setFileAddress(examineListVoResponse.getFileAddress());
            	
            	examineListVoRespList.add(examineListVoResp);
            }
            
        	//检查列表入库
            requestVo.setAppId(appId);
            logger.info("listCheckbody requestVo={}",new Object[]{requestVo});
        	Thread  getExamineListThread = new Thread(new ExamineListRunnable(examineListVoResponseList,requestVo));
        	getExamineListThread.start();
        }
        
        response.setResultCodeAndMessageAndResult("0","成功",examineListVoRespList);
        logger.info("【检查结果列表查询】结束, 返回结果=" + JsonUtils.toJsonString(response));
        return response;
    }
    
    /**
	    检查结果明细查询接口
	*/
	public ResponseObject listCheckbodyDetail(String data) {
	
		logger.info("检查结果明细 listCheckbodyDetail data: "+data);
		ResponseObject response = new ResponseObject();
		ExaminationVo requestVo = JsonUtils.parseObject(data, ExaminationVo.class);
		
		if (StringUtils.isBlank(requestVo.getHospitalCode())
		       ||StringUtils.isBlank(requestVo.getCheckId())){
		   response.setResultCodeAndMessage("18002","hospitalCode,checkId不能为空");
		   return response;
		}
		
		//结果集 检查明细信息
		ExamineDetailVoResponse examineDetailVoResponse = null;
		
		//检查明细查询
		try {
			
		   ExamineDetailVo examineDetailVo = new ExamineDetailVo();
	   	   examineDetailVo.setHospitalCode(requestVo.getHospitalCode());
	   	   examineDetailVo.setBranchCode(requestVo.getBranchCode());
	   	   examineDetailVo.setCheckId(requestVo.getCheckId());
	       examineDetailVo.setCheckType(requestVo.getCheckType());
	       
	       logger.info("调用调度中心开始");
	       ResCommon<ExamineDetailVoResponse> examineDetail  = HisifaceService.getHisifaceClient().getExamineDetail(examineDetailVo);
	       logger.info("调用调度中心结束");
	       if (!"0".equals(examineDetail.getResultCode())){
	           logger.info("调用调度中心产生异常"+ examineDetail.getResultMessage());
	           response.setResultCodeAndMessage("18998", examineDetail.getResultMessage());
	           return response;
	       }
	       logger.info("调用调度中心返回 "+JsonUtils.toJsonString(examineDetail));
	       examineDetailVoResponse = examineDetail.getResult();
	       
		} catch (Exception ex) {
		   response.setResultCodeAndMessage("18999","系统异常");
		   return  response;
		}
		
		if(examineDetailVoResponse != null){
			//检查列表入库
			Thread  getExamineListAndDetailThread = new Thread(new ExamineDetailRunnable(examineDetailVoResponse,requestVo));
			getExamineListAndDetailThread.start();
		}
		
		response.setResultCodeAndMessageAndResult("0","成功",examineDetailVoResponse);
		logger.info("【检查结果明细查询】结束, 返回结果=" + JsonUtils.toJsonString(response));
		return response;
	}
}
